#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "./chttp-demo/http.h"
#include "./cJsonDemo/cJSON.h"
#include <string.h>

int main(int argc, char const *argv[])
{

    char *url = "http://apis.juhe.cn//simpleWeather/query?city=%E5%B9%BF%E5%B7%9E&key=ad1fcfc94b50ff2b7fac1c4e1d4568c7";
    http_init(url);
    http_setRawHeader("Host", "apis.juhe.cn");
    http_setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    http_get();

    char *replyData;
    http_reply(&replyData);
    // printf("replyData:%s\n", replyData);
    // for (int i = 0; i < 1000; i++)
    // {
    //     printf("%d=%d=%c\n", i, replyData[i], replyData[i]);
    // }

    // char *p = index(replyData, '\n');
    // printf("p:%s\n", p);
    char *delim = "\r\n";
    char *p;
    // printf("%s ", strtok(replyData, delim));
    strtok(replyData, delim);
    p = strtok(NULL, delim);
    printf("========%s\n", p);

    // =============
    cJSON *root = cJSON_Parse(p);
    cJSON *reason = cJSON_GetObjectItem(root, "reason");
    printf("reason:%s\n", reason->valuestring);

    cJSON *result = cJSON_GetObjectItem(root, "result");
    cJSON *future = cJSON_GetObjectItem(result, "future");

    int len = cJSON_GetArraySize(future);
    printf("len:%d\n", len);
    for (int i = 0; i < len; i++)
    {
        cJSON *futureItem = cJSON_GetArrayItem(future, i);
        printf("[%d] %s\n", i, cJSON_GetObjectItem(futureItem, "date")->valuestring);
    }

    http_destory();

    // ========================

    return 0;
}
